package br.net.woodstock.tickettoride.spring.web.error;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.jsp.PageContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import br.net.woodstock.rockframework.web.struts2.Struts2Constants;

@Controller
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class InternalError extends AbstractError {

	private static final long	serialVersionUID	= 2649099545648387125L;

	private Exception			exception;

	private String				stackTrace;

	@Override
	public String execute() throws Exception {
		PageContext context = ServletActionContext.getPageContext();
		this.exception = context.getException();
		if (this.exception != null) {
			this.setMessage(this.exception.getMessage());
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			this.exception.printStackTrace(pw);
			pw.close();
			this.stackTrace = sw.getBuffer().toString();
		}
		return Struts2Constants.SUCCESS;
	}

	public Exception getException() {
		return this.exception;
	}

	public String getStackTrace() {
		return this.stackTrace;
	}

}
